/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controllers;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.RequestDispatcher;
import java.util.Vector;

import Models.LSGia_Model;
import Models.NhatKyHT_Model;
import Beans.NhanVien;

/**
 *
 * @author Venus
 */
public class LSGia_Controller extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession();
        try {
            if (session.getAttribute("langid") == null) {
                response.sendRedirect("index.jsp");
                return;
            }
            session.removeAttribute("DSMA");
            session.removeAttribute("DSNMA");
            session.removeAttribute("item");
            int langid = (Integer) session.getAttribute("langid");
            String action = request.getParameter("action");
            if (action.equals("Show")) {
                String item = request.getParameter("item");
                LSGia_Model lsgm = new LSGia_Model();
                Vector v = new Vector();
                v = lsgm.LayDSLSG();
                request.setAttribute("DSLSG", v);
                request.setAttribute("item", item);
                RequestDispatcher rd = request.getRequestDispatcher("admin/lichsugia.jsp");
                rd.forward(request, response);
            } else if (action.equals("DeleteLSG")) {
                String malsg = request.getParameter("malsg");
                String item = request.getParameter("item");
                String MaLichSuGia[] = malsg.split("-");
                int j = 0;
                while (j < MaLichSuGia.length) {
                    LSGia_Model.DeleteLSG(MaLichSuGia[j]);

                    j = j + 1;
                }
                LSGia_Model nkm = new LSGia_Model();
                Vector v = new Vector();
                v = nkm.LayDSLSG();
                NhanVien nv = new NhanVien();
                nv = (NhanVien) session.getAttribute("nv");
                NhatKyHT_Model.GhiNhatKy(4, nv.getMaNV(), "historyprice");
                request.setAttribute("item", item);
                request.setAttribute("DSLSG", v);
                RequestDispatcher rd = request.getRequestDispatcher("admin/lichsugia.jsp");
                rd.forward(request, response);
            } else if (action.equals("SortNgayGioLSG")) {
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                LSGia_Model nkm = new LSGia_Model();
                Vector v = new Vector();
                v = nkm.SortNgayGioLSG(langid, order);
                request.setAttribute("DSLSG", v);
                request.setAttribute("item", item);
                request.setAttribute("order", order);
                request.setAttribute("test", "1");
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssortlichsugia.jsp");
                rd.forward(request, response);
            } else if (action.equals("SortMonAnLSG")) {
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                LSGia_Model nkm = new LSGia_Model();
                Vector v = new Vector();
                v = nkm.SortMonAnLSG(langid, order);
                request.setAttribute("DSLSG", v);
                request.setAttribute("item", item);
                request.setAttribute("order", order);
                request.setAttribute("test", "2");
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssortlichsugia.jsp");
                rd.forward(request, response);
            } else if (action.equals("SortDVTLSG")) {
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                LSGia_Model nkm = new LSGia_Model();
                Vector v = new Vector();
                v = nkm.SortDVTLSG(langid, order);
                request.setAttribute("DSLSG", v);
                request.setAttribute("item", item);
                request.setAttribute("order", order);
                request.setAttribute("test", "3");
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssortlichsugia.jsp");
                rd.forward(request, response);
            } else if (action.equals("SortGiaLSG")) {
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                LSGia_Model nkm = new LSGia_Model();
                Vector v = new Vector();
                v = nkm.SortGiaLSG(langid, order);
                request.setAttribute("DSLSG", v);
                request.setAttribute("item", item);
                request.setAttribute("order", order);
                request.setAttribute("test", "4");
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssortlichsugia.jsp");
                rd.forward(request, response);
            } else if (action.equals("SearchLSG")) {
                String tenmonan = request.getParameter("tenmonan");
                String tendvt = request.getParameter("tendvt");
                String gia = request.getParameter("gia");
                String item = request.getParameter("item");
                String tungay = request.getParameter("tungay");
                String tugio = request.getParameter("tugio");
                String tuphut = request.getParameter("tuphut");
                String denngay = request.getParameter("denngay");
                String dengio = request.getParameter("dengio");
                String denphut = request.getParameter("denphut");
                LSGia_Model nkm = new LSGia_Model();
                Vector v = new Vector();
                v = nkm.SearchLSG(tungay, tugio, tuphut, denngay, dengio, denphut, tenmonan, tendvt, gia, langid);
                request.setAttribute("tenmonan", tenmonan);
                request.setAttribute("tendvt", tendvt);
                request.setAttribute("gia", gia);
                request.setAttribute("item", item);
                request.setAttribute("DSLSG", v);
                request.setAttribute("tungay", tungay);
                request.setAttribute("tugio", tugio);
                request.setAttribute("tuphut", tuphut);
                request.setAttribute("denngay", denngay);
                request.setAttribute("dengio", dengio);
                request.setAttribute("denphut", denphut);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssearchlichsugia.jsp");
                rd.forward(request, response);
            } else if (action.equals("SortSearchNgayGioLSG")) {
                String tenmonan = request.getParameter("tenmonan");
                String tendvt = request.getParameter("tendvt");
                String gia = request.getParameter("gia");
                String item = request.getParameter("item");
                String tungay = request.getParameter("tungay");
                String tugio = request.getParameter("tugio");
                String tuphut = request.getParameter("tuphut");
                String denngay = request.getParameter("denngay");
                String dengio = request.getParameter("dengio");
                String denphut = request.getParameter("denphut");
                String order = request.getParameter("order");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                LSGia_Model nkm = new LSGia_Model();
                Vector v = new Vector();
                v = nkm.SortSearchNgayGioLSG(tungay, tugio, tuphut, denngay, dengio, denphut, tenmonan, tendvt, gia, langid, order);
                request.setAttribute("DSLSG", v);
                request.setAttribute("item", item);
                request.setAttribute("order", order);
                request.setAttribute("test", "1");
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                request.setAttribute("tenmonan", tenmonan);
                request.setAttribute("tendvt", tendvt);
                request.setAttribute("gia", gia);
                request.setAttribute("tungay", tungay);
                request.setAttribute("tugio", tugio);
                request.setAttribute("tuphut", tuphut);
                request.setAttribute("denngay", denngay);
                request.setAttribute("dengio", dengio);
                request.setAttribute("denphut", denphut);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssortsearchlichsugia.jsp");
                rd.forward(request, response);
            } else if (action.equals("SortSearchMonAnLSG")) {
                String tenmonan = request.getParameter("tenmonan");
                String tendvt = request.getParameter("tendvt");
                String gia = request.getParameter("gia");
                String item = request.getParameter("item");
                String tungay = request.getParameter("tungay");
                String tugio = request.getParameter("tugio");
                String tuphut = request.getParameter("tuphut");
                String denngay = request.getParameter("denngay");
                String dengio = request.getParameter("dengio");
                String denphut = request.getParameter("denphut");
                String order = request.getParameter("order");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                LSGia_Model nkm = new LSGia_Model();
                Vector v = new Vector();
                v = nkm.SortSearchMonAnLSG(tungay, tugio, tuphut, denngay, dengio, denphut, tenmonan, tendvt, gia, langid, order);
                request.setAttribute("DSLSG", v);
                request.setAttribute("item", item);
                request.setAttribute("order", order);
                request.setAttribute("test", "2");
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                request.setAttribute("tenmonan", tenmonan);
                request.setAttribute("tendvt", tendvt);
                request.setAttribute("gia", gia);
                request.setAttribute("tungay", tungay);
                request.setAttribute("tugio", tugio);
                request.setAttribute("tuphut", tuphut);
                request.setAttribute("denngay", denngay);
                request.setAttribute("dengio", dengio);
                request.setAttribute("denphut", denphut);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssortsearchlichsugia.jsp");
                rd.forward(request, response);
            } else if (action.equals("SortSearchDVTLSG")) {
                String tenmonan = request.getParameter("tenmonan");
                String tendvt = request.getParameter("tendvt");
                String gia = request.getParameter("gia");
                String item = request.getParameter("item");
                String tungay = request.getParameter("tungay");
                String tugio = request.getParameter("tugio");
                String tuphut = request.getParameter("tuphut");
                String denngay = request.getParameter("denngay");
                String dengio = request.getParameter("dengio");
                String denphut = request.getParameter("denphut");

                String order = request.getParameter("order");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                LSGia_Model nkm = new LSGia_Model();
                Vector v = new Vector();
                v = nkm.SortSearchDVTLSG(tungay, tugio, tuphut, denngay, dengio, denphut, tenmonan, tendvt, gia, langid, order);
                request.setAttribute("DSLSG", v);
                request.setAttribute("item", item);
                request.setAttribute("order", order);
                request.setAttribute("test", "3");
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                request.setAttribute("tenmonan", tenmonan);
                request.setAttribute("tendvt", tendvt);
                request.setAttribute("gia", gia);
                request.setAttribute("tungay", tungay);
                request.setAttribute("tugio", tugio);
                request.setAttribute("tuphut", tuphut);
                request.setAttribute("denngay", denngay);
                request.setAttribute("dengio", dengio);
                request.setAttribute("denphut", denphut);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssortsearchlichsugia.jsp");
                rd.forward(request, response);
            } else if (action.equals("SortSearchGiaLSG")) {
                String tenmonan = request.getParameter("tenmonan");
                String tendvt = request.getParameter("tendvt");
                String gia = request.getParameter("gia");
                String item = request.getParameter("item");
                String tungay = request.getParameter("tungay");
                String tugio = request.getParameter("tugio");
                String tuphut = request.getParameter("tuphut");
                String denngay = request.getParameter("denngay");
                String dengio = request.getParameter("dengio");
                String denphut = request.getParameter("denphut");
                String order = request.getParameter("order");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                LSGia_Model nkm = new LSGia_Model();
                Vector v = new Vector();
                v = nkm.SortSearchGiaLSG(tungay, tugio, tuphut, denngay, dengio, denphut, tenmonan, tendvt, gia, langid, order);
                request.setAttribute("DSLSG", v);
                request.setAttribute("item", item);
                request.setAttribute("order", order);
                request.setAttribute("test", "4");
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                request.setAttribute("tenmonan", tenmonan);
                request.setAttribute("tendvt", tendvt);
                request.setAttribute("gia", gia);
                request.setAttribute("tungay", tungay);
                request.setAttribute("tugio", tugio);
                request.setAttribute("tuphut", tuphut);
                request.setAttribute("denngay", denngay);
                request.setAttribute("dengio", dengio);
                request.setAttribute("denphut", denphut);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssortsearchlichsugia.jsp");
                rd.forward(request, response);
            }
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
